package com.kobeliu.sharding_demo.strategy;

import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.*;

public class CustomTableRangeShardingAlgorithm implements RangeShardingAlgorithm<Long> {
    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Long> rangeShardingValue) {

        Set<String> result = new LinkedHashSet<>();

        //先找起始值
        long start = rangeShardingValue.getValueRange().lowerEndpoint();
        //重点
        long end = rangeShardingValue.getValueRange().upperEndpoint();

        for (long i = start; i <= end ; i++) {
            for (String table : collection) {
                if(table.endsWith(i%collection.size() + "")){
                    result.add(table);
                }
            }
        }

        return result;
    }
}
